<?php
/**
 * Actions related to the homepage of the site.
 *
 * @package    actions
 * @subpackage home
 */
class taskActions extends sfGraduaActions
{

    protected $treeJsInstanceName = 'tasks.tree';

    public function executeShowHistory() {
        if(!$this->hasRequestParameter('id')) die('ID required');
        $this->history = DevTaskTable::loadHistory($this->getRequestParameter('id'));
    }

    public function executeShowNodeData() {
        if(!$this->hasRequestParameter('id')) die('ID required');
        $node = TasksService::get($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->getResponseJsCall()
            ->addCommand('extraMenu','displayBreadcrumbs')
            ->addPHPParam(
                'data',
                partial2Js( 'task/breadcrumbs' , array('node'=>$node), false )
            )
            ->addCommand('tasks.task','displayNodeData')
            ->addPHPParam(
                'data',
                partial2Js( 'task/nodeData' , array('node'=>$node), false )
            )
            ->addCommand('tasks.tree','deselect')
            ->addCommand('tasks.tabs','select');
        $tab = $this->getRequestParameter('tab',null);
        if($tab) {
            $this->getResponseJsCall()
            ->addPHPParam('tab', (int)$tab);
        }
        return sfView::NONE;
    }
    
    public function executeShowEditTask() {
        if(!$this->hasRequestParameter('id')) die('ID required');
        $title = "Edit Task";
        sfLoader::loadHelpers(array('Partial','Url'));
        $task = Doctrine::getTable('DevTask')->find($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        return $this->showJsCall(
            JsCall::build()
            ->addCommand('TaskC','displayNodeContent')
            ->addPhpParam(
                'data',
                partial2Js(
                    'task/taskForm',
                    array(
                      'node'=>$task,
                      'title'=>$title,
                      'action'=>'updateTask'
                    ),
                    false
                )
            )
        );
    }

    /*public function executeUpdateTask() {
        $id = $this->getRequestParameter('id');
        $task = Doctrine::getTable('DevTask')->find($id);
        $task->name = ($this->getRequestParameter('name'));
        $task->description = ($this->getRequestParameter('description'));
        $task->how_to_show = ($this->getRequestParameter('how_to_show'));
        $task->technical_aspects = ($this->getRequestParameter('technical_aspects'));
        $e = $this->getRequestParameter('estimated',null);
        $task->estimated = $e ? number_format($e,2) : null;
        $task->complete = ($this->hasRequestParameter('complete'));
        $task->backlog = ($this->hasRequestParameter('backlog'));
        $task->save();
        $r = $this->getResponse();
        $r->setContent(
            JsCall::build()
            ->addCommand('TaskC','showNodeData')
            ->addPHPParam('id',$id)
            ->addCommand('TaskTreeC','refreshList')
            ->addPHPParam('id',$id)
            ->toJson()
        );
        DevTaskTable::sumTestsByTask();
        $this->setLayout(false);
        return sfView::NONE;
    }*/

    function executeShowEditState($request) {
        // 1 - lê do serviço
        $item = TasksService::get($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->getResponseJsCall()
            ->displayDialog(
                partial2Js(
                    'task/editStateForm',
                    array(
                      'task'=>$item
                    ),
                    false
                )
            )
            ->addPhpParam('width',500)
            ->addPhpParam('height',300);
        return sfView::NONE;
    }

    function executeEditState($request) {
        $obj = $this->getObjRequest('task');
        if($obj->estimated == '') unset($obj->estimated);
        TasksService::edit($obj);
        $this->getResponseJsCall()
            ->addCommand('tasks.task', 'showNodeData')
            ->addPHPParam('id', $obj->id)
            ->addCommand('UtilC', 'closeDialog')
            ->addCommand('tasks.tree','refresh');
        return sfView::NONE;
    }

    function executeShowEditDescription($request) {
        // 1 - lê do serviço
        $item = TasksService::get($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->getResponseJsCall()
            ->displayDialog(
                partial2Json('task/editDescriptionForm',array('task'=>$item))
            )
            ->addPhpParam('width',850)
            ->addPhpParam('height',300);
        return sfView::NONE;
    }

    function executeEditDescription($request) {
        $obj = $this->getObjRequest('task');
        TasksService::edit($obj);
        $this->getResponseJsCall()
            ->addCommand('tasks.task', 'showNodeData')
            ->addPHPParam('id', $obj->id)
            ->addPHPParam('tab', 1)
            ->addCommand('UtilC', 'closeDialog');
        return sfView::NONE;
    }

    function executeShowEditHowToShow($request) {
        $item = TasksService::get($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->getResponseJsCall()
            ->displayDialog(
                partial2Json('task/editHowToShowForm',array('task'=>$item))
            )
            ->addPhpParam('width',850)
            ->addPhpParam('height',300);
        return sfView::NONE;
    }

    function executeEditHowToShow($request) {
        $obj = $this->getObjRequest('task');
        TasksService::edit($obj);
        $this->getResponseJsCall()
            ->addCommand('tasks.task', 'showNodeData')
            ->addPHPParam('id', $obj->id)
            ->addPHPParam('tab', 2)
            ->addCommand('UtilC', 'closeDialog');
        return sfView::NONE;
    }

    function executeShowEditTechnicalAspects($request) {
        $item = TasksService::get($this->getRequestParameter('id'));
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->getResponseJsCall()
            ->displayDialog(
                partial2Json('task/editTechnicalAspectsForm', array('task'=>$item))
            )
            ->addPhpParam('width',850)
            ->addPhpParam('height',300);
        return sfView::NONE;
    }

    function executeEditTechnicalAspects($request) {
        $obj = $this->getObjRequest('task');
        TasksService::edit($obj);
        $this->getResponseJsCall()
            ->addCommand('tasks.task', 'showNodeData')
            ->addPHPParam('id', $obj->id)
            ->addPHPParam('tab', 3)
            ->addCommand('UtilC', 'closeDialog');
        return sfView::NONE;
    }

    public function executeShowRelateTests() {
        $this->setLayout('layoutTasks');
        $dir = '../test/phpunit';
        $tests = array();
        $taskId = $this->getRequestParameter('id');
        $res = TasksService::getRelatedTests($taskId);
        $res['taskId'] = $taskId;
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        return $this->showJsCall(
            JsCall::build()
            ->addCommand('tasks.task','displayRelatedTests')
            ->addPHPParam(
                'data',
                partial2Js(
                    'task/relateTests',
                    $res,
                    false
                )
            )
        );
    }

    public function executeRelateTests() {
        $projectId = $this->getUser()->getAttribute('projectId');
        $taskId = $this->getRequestParameter('taskId');
        $tests = $this->getRequestParameter('arTaskTest',array());
        $node = TasksService::get($taskId);
        DevTaskTable::relateTests($taskId, $tests);
        TasksService::calculateResultsAndStates($projectId);
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        return $this->showJsCall(
            JsCall::build()
            ->addCommand('tasks.task','showNodeData')
            ->addPHPParam(
                'id',
                $node['id']
            )
            ->addPhpParam('tab',4)
            ->addCommand('tasks.tree','refresh')
            ->addPHPParam('id',$taskId)
        );
    }

}